//
//  ModifyUserInfoViewController.m
//  AppointmentForCar
//
//  Created by Herron on 2016/11/14.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "ModifyUserInfoViewController.h"
#import "ProvinceListViewController.h"
#import "CameraTakeMamanger.h"
#import "QRCodeViewController.h"
#import "CityModel.h"

@interface ModifyUserInfoViewController ()

@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *nicknameTextField;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;
@property (weak, nonatomic) IBOutlet UIImageView *avatarImageView;
@property (nonatomic, copy) NSString *avatarUrl;            // 头像
@property (nonatomic, strong) CityModel *city;               // 城市
@property (nonatomic, copy) NSString *gender;               // 性别
@property (weak, nonatomic) IBOutlet UIButton *maleBtn;
@property (weak, nonatomic) IBOutlet UIButton *femaleBtn;
@property (nonatomic, copy) NSString *qrcode;

@end

@implementation ModifyUserInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"完善资料";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveClick:)];
    
    NSString *userId = [UserAccountManager shareUserManager].user.userId;
    [NetWork postNetWorkWithUrl:kURLUserInfo parame:@{@"id":userId} successBlock:^(NSDictionary *dic) {
        NSLog(@"dic = %@", dic);
        UserModel *user = [UserModel mj_objectWithKeyValues:dic[@"data"][@"data"]];
        self.qrcode = user.qrcode;
        self.phoneLabel.text = user.phoneNumber;
        self.nicknameTextField.text = user.nickName;
        self.cityLabel.text = user.address;
        [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, user.avatarUrl]]];
        self.gender = user.gender;
        if ([self.gender isEqualToString:@"男"]) {
            [self.maleBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
            [self.femaleBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
        } else {
            [self.femaleBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
            [self.maleBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
        }
    } errorBlock:^(id error) {
        [HUDManager showWarningWithText:@"网络异常"];
    }];
    
    NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
    [notification addObserver:self selector:@selector(selectCity:) name:@"sendCityTwo" object:nil];

}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - <CityListViewControllerDelegate>
- (void)selectCity:(NSNotification *)notification {
    NSDictionary *dic = notification.userInfo;
    CityModel *city = [[CityModel alloc] initWithDictionary:dic error:nil];
    self.cityLabel.text = city.name;
    self.city = city;
}


#pragma mark - event mothed
// 保存
- (void)saveClick:(id)sender {
    if (self.nicknameTextField.text.length) {
        [NetWork postNetWorkWithUrl:kURLChangeNickName parame:@{@"nickName":self.nicknameTextField.text} successBlock:^(NSDictionary *dic) {
            NSLog(@"昵称dic = %@", dic);
            [UserAccountManager shareUserManager].user.nickName = self.nicknameTextField.text;
            // 通知
            
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:kNoticeChangeName object:nil userInfo:dic];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"修改昵称失败"];
        }];
    }
    
    if (self.avatarUrl.length) {
        [NetWork postNetWorkWithUrl:kURLChangeAvatar parame:@{@"avatarUrl":self.avatarUrl} successBlock:^(NSDictionary *dic) {
            NSLog(@"头像dic = %@", dic);
            [UserAccountManager shareUserManager].user.avatarUrl = self.avatarUrl;
            // 通知
            NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
            [center postNotificationName:kNoticeChangeAvatarImage object:nil userInfo:dic];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"修改头像失败"];
        }];
    }
    if (self.city.cityId.length) {
        NSString *userId = [UserAccountManager shareUserManager].user.userId;
        [NetWork postNetWorkWithUrl:kURLChangeAddress parame:@{@"id":userId, @"type":@"1", @"cityId":self.city.cityId} successBlock:^(NSDictionary *dic) {
            NSLog(@"地区dic = %@", dic);
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"修改地区失败"];
        }];
    }
    
    if (self.gender.length) {
        [NetWork postNetWorkWithUrl:kURLChangeGender parame:@{@"gender":self.gender} successBlock:^(NSDictionary *dic) {
            NSLog(@"性别dic = %@", dic);
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"修改性别失败"];
        }];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

// 地区
- (IBAction)cityClick:(id)sender {
    ProvinceListViewController *vc = [[ProvinceListViewController alloc] init];
    vc.type = @"2";
    [self.navigationController pushViewController:vc animated:YES];
}

// 头像
- (IBAction)uploadAvatarClick:(id)sender {
    [[CameraTakeMamanger sharedInstance] cameraSheetInController:self handler:^(UIImage *image, NSString *imagePath) {
        NSLog(@"imagePath = %@", imagePath);
        [NetWork uploadImageWithUrl:kURLUploadImage image:image imageName:@"avatar" successBlock:^(NSDictionary *dic) {
            NSLog(@"dic = %@", dic);
            NSArray *urls = dic[@"data"][@"urls"];
            self.avatarUrl = [urls firstObject];
            [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", kRootImageUrl, self.avatarUrl]] placeholderImage:[UIImage imageNamed:@"registerAvatar"]];
        } errorBlock:^(id error) {
            [HUDManager showWarningWithText:@"上传失败"];
        }];
    }];
}

// 性别
- (IBAction)genderClick:(UIButton *)sender {
    if (sender.tag == 1) {
        self.gender = @"男";
        [sender setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.femaleBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    } else {
        self.gender = @"女";
        [sender setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
        [self.maleBtn setImage:[UIImage imageNamed:@"noSelect"] forState:UIControlStateNormal];
    }
}

- (IBAction)qrCodeClick:(id)sender {
    QRCodeViewController *vc = [[QRCodeViewController alloc] init];
    vc.code = self.qrcode;
    [self.navigationController pushViewController:vc animated:YES];
}


@end
